package com.bwie.controller;

import com.bwie.pojo.Car;
import com.bwie.pojo.Images;
import com.bwie.service.CarService;
import com.bwie.service.ImagesService;
import com.bwie.utils.OSSFileUtil;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.IOException;
import java.util.List;

/**
 * @Auther:${涛}
 * @Date:2024/9/3 15:06
 * @Description: CarController
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user")
public class CarController {


    @Autowired
    private CarService carService;
    @Autowired
    private ImagesService imagesService;


    //图片上传
    @PostMapping("/file")
    public R file(MultipartFile file) throws IOException {
        OSSFileUtil ossFileUtil = new OSSFileUtil();
        String url = ossFileUtil.uploadFile(file);
        return R.OK(url);
    }


    @PostMapping("/CarList")
    public R carList() {
        List<Car> list = carService.Carlist();
        return R.OK(list);
    }


    @PostMapping("/CarAdd")
    public R carAdd(@RequestBody Car car) {

        String text = "1,2,3,4";

        String[] split = text.split(",");



        for (String s : split) {
            System.out.println(s);
        }
        //切割图片
        List<Images> imageList = car.getImageList();

        carService.save(car);

        for (Images images : car.getImageList()) {

            images.setCarId(car.getId());
            imagesService.save(images);
        }

        return R.OK();

    }
}


